package cn.ysym.util;

import android.app.Notification;
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class CommonUtil {
    private CommonUtil() {
    }

    public static String getSoftVersion() {
        return "2.5.20200513.a.tcsy";
    }

    /**
     * 按给定格式获取当前时间字符串
     *
     * @param format
     * @return
     */
    public static String getDateTimeStr(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(new Date());
        return dateString;
    }

    public static int playSound(final Context context) {
        //播放系统提示音
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone rt = RingtoneManager.getRingtone(context, uri);
        rt.play();

        Notification nt = new Notification();
        nt.defaults = Notification.DEFAULT_SOUND;
        int soundId = new Random(System.currentTimeMillis()).nextInt(Integer.MAX_VALUE);
        /**
         * 下行代码报错
         * java.lang.IllegalArgumentException: Invalid notification (no valid small icon)
         */
/*
        mgr.notify(soundId, nt);
*/
        return soundId;
    }

    public static String cleanCode(String code) {
        String retCode = code;
        int tIndex = retCode.lastIndexOf("=");
        if (tIndex >= 0) retCode = retCode.substring(tIndex + 1);

        tIndex = retCode.lastIndexOf("/");
        if (tIndex >= 0) retCode = retCode.substring(tIndex + 1);

        return retCode;
    }

    /**
     * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     *
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map<String, String> params) {

        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (value != null && !value.trim().equals("")) {
                if (sb.length() > 0) sb.append("&");
                sb.append(key + "=" + value);
            }
        }

        return sb.toString();
    }

    /**
     * MD5摘要
     *
     * @param src
     * @return
     */
    public static String md5Digest(String src) {
        return md5Digest(src, "utf-8");
    }

    /**
     * MD5摘要
     *
     * @param src
     * @return
     */
    public static String md5Digest(String src, String charSet) {
        if (src == null)
            return "";
        String rst;
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            md.update(src.getBytes(charSet));
            byte[] dig = md.digest();
            rst = bytes2LowerHex(dig);
        } catch (Exception e) {
            rst = src;
        }
        return rst;
    }

    public static String bytes2LowerHex(byte[] source) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < source.length; i++) {
            int digByte = source[i] & 0xFF;
            if (digByte <= 0x0F)
                sb.append("0");
            sb.append(Integer.toHexString(digByte));
        }
        return sb.toString().toLowerCase();
    }

    /**
     * 判断一个或多个对象,只要其中一个为空就返回true 否则返回false<br/>
     * <h4>注意</h4> String长度为0：<strong>return true</strong> <br/>
     * Integer<1 ：<strong>return true</strong>
     *
     * @param o
     * @return 空:true，否则 ：false.
     */
    public static boolean isEmpty(Object... o) {
        if (null == o)
            return true;
        for (Object obj : o) {
            if (null == obj)
                return true;
            if (obj instanceof String
                    && String.valueOf(obj).trim().length() == 0)
                return true;
            if (obj instanceof Integer
                    && Integer.valueOf(String.valueOf(obj)) < 1)
                return true;
        }
        return false;
    }

    public static String formatStr(String str) {

        if (str.length() > 35)
            str = str.substring(0, 17) + "\n" + str.substring(17, 34) + "\n" + str.substring(34);

        else if (str.length() > 18 && str.length() <= 35)
            str = str.substring(0, 17) + "\n" + str.substring(17);

        else return str;

        return str;
    }

    /**
     * 获取指定格式id
     *
     * @param data
     * @return
     */
    public static String getDataId(String data) {
        int index = data.indexOf(" ");
        if (index > 0) {
            data = data.substring(0, index);
        } else return data;

        return data;
    }

    /**
     * 获取指定格式后缀
     *
     * @param data
     * @return
     */
    public static String getDataBack(String data) {
        int index = data.indexOf(" ");
        if (index > 0) {
            data = data.substring(index+1);
        } else return data;

        return data;
    }
}
